import config from './config.js'
import  tools from './tools.js'
const request={
    request(options){
      return new Promise((resolve, reject) => {
        let showLoading=true
        if(options.data && options.data.hasOwnProperty('showLoading') && options.data.showLoading===0){
            showLoading=false
            delete options.data.showLoading
        }
        if(showLoading)  uni.showLoading();
        uni.request({
          url: `${config.baseURL}${options.url}`,
          method: options.method || 'GET',
          header: {
            "Authori-zation":'Bearer' +' '+tools.getToken()
          },
          data: options.data,
          success: function(res) {
              uni.hideLoading();
            let {status,msg} = res.data
            if(status!==200){
              tools.exceptionResponse(res.data)
              reject(msg)
            }else{
              resolve(res.data)
            }
          },
          fail: reject,
        })
      })
    },
    get(url,params){
      return request.request(Object.assign({},{method:'GET',url:url},{data:params}));
    },
    post(url,params){
      return request.request(Object.assign({},{method:'POST',url:url},{data:params}));
    },
    put(url,params){
      return request.request(Object.assign({},{method:'PUT',url:url},{data:params}));
    },
    delete(url,params){
      return request.request(Object.assign({},{method:'DELETE',url:url},{data:params}));
    }
}
export default request
